<?php

$minical_year = $year;
$minical_month = $month;
$minical_current_day = $current_day;

$system_year = date('Y', $date);
$system_month = date('m', $date);
$system_day = date('d', $date);

?>


<?php 

$first_day = mktime(0,0,0,$minical_month, 1, $minical_year);
$num_days = cal_days_in_month(0, $minical_month, $minical_year);

// $day_counter: begins from 1, extends to last day of month ($num_days)
// $day_header: counter to keep track of which day of the week; 1->Sun, 7-> Sat
//$offset: counter to determine the number of blank days in the beginning of each month
$day_counter = 1;
$day_header = 1;

$day_of_week = date('D', $first_day);
switch ($day_of_week) {
	case "Sun": $offset = 0; break;
	case "Mon"; $offset = 1; break;
	case "Tue"; $offset = 2; break;
	case "Wed"; $offset = 3; break;
	case "Thu"; $offset = 4; break;
	case "Fri"; $offset = 5; break;
	case "Sat"; $offset = 6; break;
}


if ($minical_month == 1) {
	$temp_month = 12;
	$temp_year = $minical_year - 1;
}
else {
	$temp_month = $minical_month - 1;
	$temp_year = $minical_year;
}	
$prev_month_offset = cal_days_in_month(0, $temp_month, $minical_year);

?>

<CENTER>
<TABLE width=150 height=120 cellspacing=0 cellpadding=0>
<TR> <TH colspan=7 class="minicalheader"> <?php echo "$month_name $minical_year" ?> </TH> </TR>
<TR> <TH width=21 class="minicaldisplay">S</TH> <TH width=21 class="minicaldisplay">M</TH> <TH width=21 class="minicaldisplay">Tu</TH> <TH width=21 class="minicaldisplay">W</TH> <TH width=21 class="minicaldisplay">Th</TH> <TH width=21 class="minicaldisplay">F</TH> <TH width=21 class="minicaldisplay">S</TH></TR>
<TR>
	<?php
	
	$counter = 0;
	while ($offset > 0)
	{	
		if ($counter == 0 || $counter == 6) {
			echo "<TD align=center valign=middle bgcolor='DDDDDD' style='font-size:9px; color:silver;'>";
			if ($counter == 6)
				$counter = 0;
			else
				$counter++;
		}
		else {
			echo "<TD align=center valign=middle style='font-size:9px; color:silver;'>";
			$counter++;
		}
		echo $prev_month_offset - $offset+1;
		echo "</TD>";
		$offset--;
		$day_counter++;
	}
		
	while ($day_header <= $num_days) 
	{
		if ($day_header == $system_day && $minical_month == $system_month && $minical_year == $system_year)
		{
			echo "<TD align=center valign=middle bgcolor=silver class='minicaldisplay'>";
			echo "<a href='viewDay.php?day=$day_header&month=$minical_month&year=$minical_year' class='minicallink'>$day_header</a>";
			if ($counter == 6)
				$counter = 0;
			else
				$counter++;
		}
		else
		{
			if ($counter == 0 || $counter == 6) {
				echo "<TD align=center valign=middle bgcolor='DDDDDD' class='minicaldisplay'>";
				if ($counter == 6)
					$counter = 0;
				else
					$counter++;
			}
			else {
				echo "<TD align=center valign=middle class='minicaldisplay'>";
				$counter++;
			}
			echo "<a href='viewDay.php?day=$day_header&month=$minical_month&year=$minical_year' class='minicallink'>$day_header</a>";
		}
		echo "</TD>";
		$day_header++;
		$day_counter++;
		if ($day_counter > 7)
			{
			echo "</TR><TR>";
			$day_counter = 1;
			}
	}
	$next_offset = 1;
	while ($day_counter <= 7 && $day_counter != 1) 
	{
		if ($counter == 0 || $counter == 6) {
			echo "<TD align=center valign=middle bgcolor='DDDDDD' style='font-size:9px; color:silver;'>";
			if ($counter == 6)
				$counter = 0;
			else
				$counter++;
		}
		else {
			echo "<TD align=center valign=middle style='font-size:9px; color:silver;'>";
			$counter++;
		}
		echo $next_offset;
		echo "</TD>";
		$day_counter++;
		$next_offset++;
	}
	?>
</TR>
</TABLE>
</CENTER>
